<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 首页广告管理
 * @filename admin_adv.php
 * @author hmw
 *
 */
class Admin_adv extends MY_Controller {

	function Admin_adv()
	{
		parent::MY_Controller();
		parent::check_admin_login();
	}

	function index()
	{
		self::adv_list();
	}
	
	function adv_list(){
		$this->load->model('Madv');
		$advs = $this->Madv->get_adv_list();
		$this->template->view('admin/adv_list',array('advs'=>$advs));
	}
	
	function adv_new(){
		$this->template->view('admin/adv_add');
	}
	
	function adv_add(){
		$config['upload_path'] 		= $this->config->item('upload_uri').'adv/';
		$config['allowed_types'] 	= $this->config->item('adv_img_types');
		$config['max_size'] 		= $this->config->item('adv_img_max_size');
		//$config['max_width'] 		= $this->config->item('adv_img_width');
		//$config['max_height'] 		= $this->config->item('adv_img_height');
		if(!file_exists($config['upload_path'])){
			mkdir($config['upload_path'],0777);
		}
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('adv_img')) {
			echo '文件上传失败，可能原因为：<br />'.$this->uplaod->display_errors();
		} else {
			$form = array('adv_name','adv_link','adv_state');
			foreach($form as $val){
				$$val = $this->input->post($val);
			}
			if (!$adv_link){
				echo '必须填写广告链接';return ;
			}
			$adv_name = $adv_name ? $adv_name : '首页广告';
			$values = array();
			foreach($form as $val){
				$values[$val] = $$val;
			}
			$data = $this->upload->data();
			$values['adv_img'] = $data['file_name'];
			$this->load->model('Madv');
			if ($this->Madv->insert_new_adv($values)) {
				echo '首页广告添加成功！';
			} else {
				echo '首页广告添加失败！';
			}
		}
	}
	
	function adv_edit($id){
		$this->load->model('Madv');
		$info = $this->Madv->get_adv_info($id);
		$this->template->view('admin/adv_edit',array('info'=>$info));
	}
	
	function adv_modify(){
		$form = array('id','adv_name','adv_link','adv_state');
		$values = array();
		foreach($form as $val){
			$$val = $this->input->post($val);
			$values[$val] = $$val;
		}
		
		$config['upload_path'] 		= $this->config->item('upload_uri').'adv/';
		$config['allowed_types'] 	= $this->config->item('adv_img_types');
		$config['max_size'] 		= $this->config->item('adv_img_max_size');
		//$config['max_width'] 		= $this->config->item('adv_img_width');
		//$config['max_height'] 		= $this->config->item('adv_img_height');
		if(!file_exists($config['upload_path'])){
			mkdir($config['upload_path'],0777);
		}
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('adv_img')) {
			//echo '文件上传失败，可能原因为：<br />'.$this->uplaod->display_errors();
		} else {
			$data = $this->upload->data();
			$img = $data['file_name'];
			$values['adv_img'] = $img;
		}
		$this->load->model('Madv');
		if ($this->Madv->update_adv_info($id,$values)) {
			echo '修改成功！';
		} else {
			echo '修改失败！';
		}
	}
	
	function adv_enable($id){
		if (!$id){
			echo '请选中广告再操作';return;
		}
		$this->load->model('Madv');
		if ($this->Madv->update_adv_state($id,1)){
			echo '成功启用，请检查是否还有其他广告。';
		} else {
			echo '启用失败';
		}
	}
	
	function adv_disable($id){
		if (!$id){
			echo '请选中广告再操作';return;
		}
		$this->load->model('Madv');
		if ($this->Madv->update_adv_state($id,0)){
			echo '成功禁用，请检查是否还有其他广告';
		} else {
			echo '禁用失败';
		}
	}
	
	function adv_del($id){
		if (!$id){
			echo '请选中广告再操作';return;
		}
		$this->load->model('Madv');
		if ($this->Madv->delete_adv($id)){
			echo '成功删除！';
		} else {
			echo '删除失败';
		}
	}
	
}

?>